from django.http import JsonResponse
from django.contrib.auth import login
from django.contrib.auth.models import User
from game.models.player.player import Player
from django.contrib.auth.models import User
from game.models.player.player import Player
from rest_framework.views import APIView
from rest_framework.response import Response


class PlayerView(APIView):
    def post(self, request):
        data = request.POST
        username = data.get("username", "").strip()
        password = data.get("password", "").strip()
        password_confirm = data.get("password_confirm", "").strip()
        if not username or not password:
            return Response({
                "result": "用户名和密码不能为空"
            })
        if password != password_confirm:
            return Response({
                "result": "两次密码不一致"
            })
        if User.objects.filter(username=username).exists():
            return Response({
                "result": "用户名已存在"
            })
        user = User(username=username)
        user.set_password(password)
        user.save()
        Player.objects.create(user=user, photo="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsports%2Ftransform%2F20161209%2FPPZE-fxypipu7514311.jpg&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649050938&t=42b43d09e8f6d3d5821e36c57ba393f8")
        # login(request, user)
        return Response({
            "result": "success"
        })


def test(req):
    print("adf")
    return JsonResponse({
        "res":"123123"
    })